﻿var sql = require( 'node-sqlserver' );
var path = require( 'path' );
var async = require('async');

var dbContext = require( path.join( __dirname, "dbContext" ) );

/*
 * 向WorkFlow插入一条数据
 */
exports.InsertWorkFlow = function ( model, successCall ) {

    dbContext.openNodeConn( function ( conn ) {

        conn.query( "insert into workflow(WorkFlowIdentify,Name,Description) values(?,?,?)",

            [model.WorkFlowIdentify, model.Name, model.Description],

            function ( err, rows ) {

                if ( err ) {
                    err = "InsertWorkFlow:" + err;
                    console.log( "Select in DB Error: " + err );
                }

                if ( successCall && typeof successCall == "function" ) { successCall(  err, rows ); }
            });

    });

};

/*
 * 检测某个流程是否已经定义
 */
exports.HasWorkFlowExisted = function( identify, successCall){
    
    dbContext.openNodeConn(function( conn ){
        conn.query( "select count(*) as count from workflow where WorkFlowIdentify = ?", [identify],function( err, rows ){
            
            if ( err ) {
                    err = "HasWorkFlowExisted:" + err;
                    console.log( "Select in DB Error: " + err );
            }

            if ( successCall && typeof successCall == "function" ) { successCall( err ,rows[0]["count"] > 0 ); }
                
        });    
    });

};

/*
 * 删除某个流程定义(包含WorkFlowNode)
 */
exports.DeleteWorkFlowByIdentify = function( identify ,successCall){

    dbContext.openNodeConn(function( conn ){
        
        async.series(
        {
            deleteNode: function( callback ){
            
                conn.query('delete a from workflownode a left join workflow b on a.workflowid = b.id where b.WorkFlowIdentify = ? ',
                        [identify],
                    function( err , rows ){

                        if( err ){
                            console.log( "Select in DB Error: " + err );
                            callback(err);
                            
                        }else{
                            callback(null,rows);    
                        }
                });
            },

            deleteWorkflow: function( callback ){

                conn.query("delete from workflow where WorkFlowIdentify = ?",[identify],function( err, rows){
            
                    if ( err ) {
                            console.log( "Select in DB Error: " + err );
                            err = "DeleteWorkFlowByIdentify:" + err;
                    }
                    callback(err,rows);

                });
            }

        },function(err, result){

            if ( err ) {
                 err = "DeleteWorkFlowByIdentify:" + err;
                 console.log( "Select in DB Error: " + err );
            }

            if ( successCall && typeof successCall == "function" ) {  successCall (err,result);}
        });

        
            
    });

};

/*
 * 根据identify获取WorkFlow
 */
exports.GetWorkFlowByIdentify = function( identify, successCall){
    
    dbContext.openNodeConn(function( conn ){
        
        conn.query( "select * from workflow where WorkFlowIdentify = ?", [identify], function( err, rows){
            
            if ( err ) {
                    console.log( "Select in DB Error: " + err );
                    err = "GetWorkFlowByIdentify:" + err;
            }
            
            var result = null;
            if( rows && rows.length > 0) result = rows[0];

            if ( successCall && typeof successCall == "function" ) { successCall (err ,result);}
        });

    });    
};
